<?php
define('ADODB_DIR','/var/www/adodb');
require_once ('/var/www/adodb/adodb.inc.php');

function conex_hel(){
	$conn_hel	= ADONewConnection('oci8');
	$cstr		= "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.181.23.146)(PORT = 1528)))";
	$cstr		.= "(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME = gestionredes)))";
	$conn_hel->Connect($cstr, 'helusr', 'helusr');
	return $conn_hel;
}

function desconex_hel($conn_hel){
	$conn_hel->Close();
}

function getImsiRange($IMSI){
	$rango_IMSI	= substr($IMSI,5,2);
	$helRst		= array();
	$qry		= "select id,substr(lower,6,2) as rango_inicio,substr(upper,6,2) as rango_final from hel.rangos_imsi";
	$hel		= conex_hel();
	$rs		= $hel->Execute($qry);
	$ID_HLR 	= "";
	while (!$rs->EOF){
		$helRst['id']		= $rs->fields[0];
		$helRst['rango_i']	= $rs->fields[1];
		$helRst['rango_f']	= $rs->fields[2];
		if ($rango_IMSI >= $helRst['rango_i'] && $rango_IMSI <= $helRst['rango_f']){
			$ID_HLR = $helRst['id'];
			break;
		}
		$rs->MoveNext();
	}
	desconex_hel($hel);
	return $ID_HLR;
}

function getHlrFromID($hlrId){
	$helRst		= array();
	$qry		= "select hlr,ip_hlr from hel.hlrs where id=".$hlrId;
	$hel		= conex_hel();
	$rs		= $hel->Execute($qry);
	while (!$rs->EOF){
		$helRst['name']		= $rs->fields[0];
		$helRst['ip']		= $rs->fields[1];
		$rs->MoveNext();
	}
	desconex_hel($hel);
	return $helRst;
}

function getApnFromID($apnId){
	$apnRst		= array();
	$qry		= "select codigo,descripcion from hel.htran where codigo = 'APNID-".$apnId."'";
	$hel		= conex_hel();
	$rs		= $hel->Execute($qry);
	while (!$rs->EOF){
		$apnRst['code']		= $rs->fields[0];
		$apnRst['name']		= $rs->fields[1];
		$rs->MoveNext();
	}
	desconex_hel($hel);
	return $apnRst;
}

function getOperatorFromMSC($MSC_NUMBER){
	$apnRst		= array();
	$qry		= "select pais, operador, central from hel.operadores where codigo_op = '".$MSC_NUMBER."'";
	$hel		= conex_hel();
	$rs		= $hel->Execute($qry);
	while (!$rs->EOF){
		$apnRst['pais']		= $rs->fields[0];
		$apnRst['operador']	= $rs->fields[1];
		$apnRst['central']	= $rs->fields[1];
		$rs->MoveNext();
	}
	desconex_hel($hel);
	return $apnRst;
}

function getEqosidFromID($eqosid){
	$helRst		= array();
	$qry		= "select hlr,ip_hlr from hel.hlrs where id=".$hlrId;
	$hel		= conex_hel();
	$rs		= $hel->Execute($qry);
	while (!$rs->EOF){
		$helRst['name']		= $rs->fields[0];
		$helRst['ip']		= $rs->fields[1];
		$rs->MoveNext();
	}
	desconex_hel($hel);
	return $helRst;
}

/*
$IMSI		= "730021600549708";
$hlrId		= getImsiRange($IMSI);
$operatorArr	= getHlrFromID($hlrId);
var_dump($operatorArr);
*/
?>
